<neq name="speclist" value="">
<table class="table table-bordered" id="goods_spec_table1">
<tr>
    <td colspan="2"><b>商品规格:</b></td>
</tr>
<volist name="speclist" id="so" key="i">
<tr>
    <td>{$so.name}:</td>
    <td>
    <volist name="so['values']" id="vv" key="k">
        <button type="button" data-spec_id='{$so.id}' data-item_id='{$vv.value}' <if condition="$vv['ck'] eq '1'">class="btn btn-success"<else/>class="btn-default"</if> >{$vv.value}</button>
    </volist>
    </td>
</tr>
</volist>
</table>
</neq>
<div id="goods_spec_table2" style="margin-top:10px;">
    <table class='table table-bordered' id='spec_input_tab'>
        <tr>
            <td><b>商品货号</b></td>
            <td><b>条码</b></td>
            <td><b>市场价</b></td>
            <td><b>销售价|价格组</b></td>
            <td><b>成本价</b></td>
            <td><b>积分</b></td>
            <td><b>重量</b></td>
            <td><b>分佣</b></td>
            <td><b>库存</b></td>
        </tr>
        <tr data-value="1" data-id="{$pro.id}">
            <input type="hidden" class="gprice" name="group[]" placeholder="存放价格组的" value="{$pro.groupsting}" />
            <input type="hidden" class="" name="product_id[]" placeholder="" value="{$pro.id}"/>
            <td><input type="text" name="products_no[]" class="form-control" value="{$pro['products_no']?$pro['products_no']:goods_no(1)}"/></td>
            <td><input type="text" name="code[]" class="form-control" value="{$pro.code}"/></td>
            <td><input text="text" name="market_price[]" class="form-control w60" value="{$pro.market_price}"/></td>
            <td><input name='sell_price[]' value='{$pro.sell_price}' class="form-control w60"  /> <button type="button" <if condition="$pro.groupsting neq null">class="btn btn-red groupprice"<else/>class="btn btn-default groupprice"</if> >价格组</button></td>
            <td><input name='cost_price[]' value='{$pro.cost_price}' class="form-control w60"  /></td>
            <td><input type="text" name="integral[]" class="form-control w60" value="{$pro.integral}"/></td>
            <td><input type="text" name="weight[]" class="form-control w60" value="{$pro.weight}"/></td>
            <td><input name='commission[]' value='{$pro.commission}' class="form-control w60"  /></td>
            <td><input name='store_nums[]' value='{$pro.store_nums}' class="form-control w60"/></td>
        </tr>
    </table>
</div>
<script>
    var goods_id = <php>echo I('goods_id')?I('goods_id'):'0'</php>;
    $(function () {
//        选择
        <php>if(I('goods_id')){</php>
           ajaxGetSpecInput(<php>echo I('goods_id')</php>);
        <php>}</php>
        $("#goods_spec_table1 button").click(function(){
            if($(this).hasClass('btn-success')){
                $(this).removeClass('btn-success');
                $(this).addClass('btn-default');
            }else{
                $(this).removeClass('btn-default');
                $(this).addClass('btn-success');
            }
            ajaxGetSpecInput(goods_id);
        });
//        删除
        $('#goods_spec_table2').on('click', 'button.del_spec', function () {
            $(this).parents('tr').remove();
        });
    });

    function ajaxGetSpecInput(goods_id)
    {
        var spec_arr = {};  // 用户选择的规格数组
        // 选中了哪些属性
        $("#goods_spec_table1  button").each(function(){
            if($(this).hasClass('btn-success'))
            {
                var spec_id = $(this).data('spec_id');
                var item_id = $(this).data('item_id');
                if(!spec_arr.hasOwnProperty(spec_id))
                    spec_arr[spec_id] = [];
                spec_arr[spec_id].push(item_id);
            }
        });
        if(!isEmptyObject(spec_arr)){
        	ajaxGetSpecInput2(spec_arr,goods_id);     // 显示下面的输入框
        }
    }
    function isEmptyObject(e) {  
        var t;  
        for (t in e)  
            return !1;  
        return !0  
    } 
    /**
     * 根据用户选择的不同规格选项
     * 返回 不同的输入框选项
     */
    function ajaxGetSpecInput2(spec_arr,goods_id)
    {

        $.ajax({
            type:'POST',
            data:{'spec_arr':spec_arr,'goods_id':goods_id},
            url:"{:U('Goods/tables')}",
            success:function(data){
                $("#goods_spec_table2").html('').append(data)
                mergeTd();
            }
        });
    }
    // 合并单元格
    function mergeTd() {
        var tab = document.getElementById("spec_input_tab"); //要合并的tableID
        var maxCol = 2, val, count, start;  //maxCol：合并单元格作用到多少列
        if (tab != null) {
            for (var col = maxCol - 1; col >= 0; col--) {
                count = 1;
                val = "";
                for (var i = 0; i < tab.rows.length; i++) {
                    if (val == tab.rows[i].cells[col].innerHTML) {
                        count++;
                    } else {
                        if (count > 1) { //合并
                            start = i - count;
                            tab.rows[start].cells[col].rowSpan = count;
                            for (var j = start + 1; j < i; j++) {
                                tab.rows[j].cells[col].style.display = "none";
                            }
                            count = 1;
                        }
                        val = tab.rows[i].cells[col].innerHTML;
                    }
                }
                if (count > 1) {    //合并，最后几行相同的情况下
                    start = i - count;
                    tab.rows[start].cells[col].rowSpan = count;
                    for (var j = start + 1; j < i; j++) {
                        tab.rows[j].cells[col].style.display = "none";
                    }
                }
            }
        }
    }
</script>